package network;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JProgressBar;

public class FileUploadClient extends JFrame implements Runnable {

	String fileName;
	Socket socket;
	long fileSize;

	String fileServerIP;
	int    fileServerPort;
	
	private JPanel contentPane;
	private JTextField textField;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JTextField serverIP;
	private JLabel lblNewLabel;
	private JLabel status;
	private JProgressBar progressBar;


	
	public FileUploadClient(String ip, int port) {
		this.fileServerIP = ip;
		this.fileServerPort = port;
		
		setTitle("파일 전송 클라이언트");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 664, 190);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getProgressBar());
		contentPane.add(getTextField());
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getServerIP());
		contentPane.add(getLblNewLabel());
		contentPane.add(getStatus());
		try{
			InetAddress ia = InetAddress.getLocalHost();
			serverIP.setText(ia.getHostAddress());
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}

	@Override
	public void run() {
		try{
			socket = new Socket(fileServerIP, fileServerPort);
			FileInputStream fis = 
				new FileInputStream(new File(textField.getText()));
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			byte[] d = new byte[1024];
			int len=0;
			int totLen=0;
			
			int pos = textField.getText().lastIndexOf("\\");
			fileName = textField.getText().substring(pos+1);
			
			dos.writeUTF(fileName);
			
			while( (len = fis.read(d) ) != -1){
				dos.write(d,0, len);
				totLen += len;
				progressBar.setValue(totLen);
				//progressBar.updateUI();
				Thread.sleep(10);
			}
			
			dos.close();
			fis.close();
			socket.close();
		}catch(Exception ex){
			
		}
		

	}
	
	public void openFile(){
		JFileChooser fc = new JFileChooser();
		int yn = fc.showOpenDialog(null);
		if (yn == JFileChooser.APPROVE_OPTION) {
			textField.setText(fc.getSelectedFile().toString());
			
			fileSize = fc.getSelectedFile().length();
			progressBar.setMaximum((int)fileSize);
			
			if(fileSize >1000){
				status.setText("file size : " + fileSize/1000 + " Kb");
			}else{
				status.setText("file size : " + fileSize + " b");
			}
		}
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(12, 10, 515, 21);
			textField.setColumns(10);
		}
		return textField;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("찾아보기");
			btnNewButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					openFile();
				}
			});
			btnNewButton.setBounds(539, 9, 97, 23);
		}
		return btnNewButton;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("파일전송");
			btnNewButton_1.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					try{
						Thread t = new Thread(FileUploadClient.this);
						t.start();
					}catch(Exception ex){
					
					}
				}
			});
			btnNewButton_1.setBounds(517, 41, 119, 44);
		}
		return btnNewButton_1;
	}

	private JTextField getServerIP() {
		if (serverIP == null) {
			serverIP = new JTextField();
			serverIP.setBounds(354, 53, 151, 21);
			serverIP.setColumns(10);
		}
		return serverIP;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("서버아이피");
			lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel.setBounds(245, 56, 97, 15);
		}
		return lblNewLabel;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("파일을 선택하세요.");
			status.setBounds(0, 127, 636, 15);
		}
		return status;
	}
	private JProgressBar getProgressBar() {
		if (progressBar == null) {
			progressBar = new JProgressBar();
			progressBar.setBounds(0, 103, 636, 14);
		}
		return progressBar;
	}
}
